/*
 * handle_socket.c
 *
 *  Created on: 2009-5-5
 *      Author: tian
 */

#include"tcpipsim.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


void handle_socket(msg *pmsg){
	sockmsg retmsg;
	memset(&retmsg,0,sizeof(sockmsg));
	struct tcpcb * p=tcp_new_tcpcb(pmsg->pid,pmsg->sock,TCP_CLOSED,NULL,pmsg->msgqid);
	if(p==NULL){
		fprintf(stderr,"can not create tcpcb\n");
		retmsg.mtype=pmsg->sock;
		retmsg.ret=-1;
	}
	else{
		retmsg.mtype=pmsg->sock;
		retmsg.ret=pmsg->sock;
		if((p->pipeid=open(pmsg->buf,O_WRONLY))<0){
			fprintf(stderr,"can not open pipe\n");
			retmsg.ret=-1;
		}
	}
	if(msgsnd(ptcpcb->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),0,0)<0){
		fprintf(stderr,"can not send message to application layer\n");
		return;
	}
}
